// pages/user/bind/unbind/index.js
import {
  getAuthCode
} from '../../../../api/user.js';
import {
  unbindCompany,
} from '../../../../api/company.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    companyId: 0,
    mobile: getApp().globalData.userInfo.mobile,
    authCode: '',
    authBtnText: '发送验证码',
    isSend: false,
    countdown: 60,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 发送验证码
   */
  handleSendAuthCode() {
    getAuthCode(this.data.mobile);
    this.countDown();
  },

  /**
   * 确认解绑
   */
  handleConfirm() {
    if (this.data.authCode !== '') {
      unbindCompany().then(() => {
        wx.showToast({
          title: '解绑成功',
        })
        let timer = setTimeout(() => {
          clearTimeout(timer);
          wx.navigateBack({
            delta: 2
          })
        }, 2000);
      })
    } else {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none'
      })
    }
  },

  /**
   * 倒计时
   */
  countDown() {
    if (this.data.countdown === 0) {
      this.setData({
        isSend: false,
        authBtnText: '重新发送',
        countdown: 60,
      })
      return
    } else {
      this.setData({
        isSend: true,
        authBtnText: `重新发送(${this.data.countdown--}s)`
      })
    }
    let timer = setTimeout(() => {
      clearTimeout(timer);
      this.countDown()
    }, 1000)
  },

  /**
   * 输入验证码
   */
  onInputAuthCode(e) {
    this.setData({
      authCode: e.detail.value.trim()
    })
  },

})